package SA.student.question;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

import SA.student.MainActivity;
import SA.student.R;
import SA.student.R.id;
import SA.student.R.layout;
import SA.student.notice.noticeActivity1;
import SA.student.shuttle.shuttleActivity1;
import SA.student.student.studentActivity1;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.os.Bundle;
import android.text.*;
import android.util.Log;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View.OnClickListener;

public class questionActivity1 extends Activity {

	ArrayList<QuestionData> arrData1;
	String id;
	String tempquestion;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.question1);


		Intent intent = getIntent();
		id = intent.getStringExtra("ID");
		

		String num = "";
		String title = "";
		String teacher = "";
		String yn = "N";
		
		int i = 0;
/*
		for (i = 0; i < 15; i++) {
			qNumber = Integer.toString(i + 1);
			qTitle = qTitle + qNumber + "�섏뼱媛��濡쒖삤��";
			qTeacher = qTeacher + qNumber;
			qAnswer = "N";

			mi = new MyItem(qNumber, qTitle, qTeacher, qAnswer);
			arItem.add(mi);
		}
		*/
		
		questionnameparse qps = new questionnameparse();
		qps.run();// 공지
		arrData1 = new ArrayList<QuestionData>();

		String[] idlist = tempquestion.split("-");

		int tempnum = Integer.parseInt(idlist[0]);//제목수 스트링 형변환
		String[] noname = idlist[1].split(">");
		for (i = 0; i < tempnum; i++) {
			String[] tempcon = noname[i].split(",");
			num = tempcon[0];
			title = tempcon[1];
			teacher = tempcon[2];
			yn = tempcon[3];
			
			arrData1.add(new QuestionData(num, title, teacher, yn));
		}

		final QuestionAdapter MyAdapter = new QuestionAdapter(this,
				R.layout.question2_list, arrData1);

		ListView MyList;
		MyList = (ListView) findViewById(R.id.list);
		MyList.setAdapter(MyAdapter);

		ImageButton questionBtn = (ImageButton) findViewById(R.id.questionbtn);

		questionBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new AlertDialog.Builder(questionActivity1.this)
						.setTitle("�좎깮�섏쓣 �좏깮�댁＜�몄슂.")
						.setItems(new String[] { "沅뚯븘��", "�댁듅湲�", "源�듅��" },
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										move_questionRegister();

									}
								}).show();
			}
		});

		MyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			Intent questionView = new Intent(questionActivity1.this,
					questionActivity2_View.class);

			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				String address = MyAdapter.getItem(position);

				startActivity(questionView);
			}

		});

		TextView noticeBtn = (TextView) findViewById(R.id.notice);

		noticeBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				noticeActivity();
			}

		});
		ImageView mainBtn = (ImageView) findViewById(R.id.mainBtn);

		mainBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				mainActivity();
			}

		});
		ImageView studentBtn = (ImageView) findViewById(R.id.studentBtn);

		studentBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				studentActivity();
			}

		});

		ImageView shuttleBtn = (ImageView) findViewById(R.id.shuttleBtn);

		shuttleBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				shuttleActivity();
			}

		});

	}

	void mainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	}

	void studentActivity() {
		Intent intent = new Intent(this, studentActivity1.class);
		startActivity(intent);
	}

	void shuttleActivity() {
		Intent intent = new Intent(this, shuttleActivity1.class);
		startActivity(intent);
	}

	void move_questionRegister() {
		Intent intent = new Intent(this, questionActivity3_Register.class);
		startActivity(intent);
	}
	
	public class questionnameparse implements Runnable {
		public void run() {

			// TODO Auto-generated method stub
			try {
				// String addr
				// ="Http://220.67.128.8/temp.php?useUnicode=true&characterEncoding=EUC-KR&tID=1234"
				// + "&adate=20120902" + "&classid="+ classid +
				// "&content="+content;

				//URL url = new URL("Http://220.67.128.8/noticecontent.php");
				URL url = new URL("Http://220.67.128.8/questionname.php?id="
						+ URLEncoder.encode(id, "EUC-KR"));
				// Log.e("1", tid);
				// Log.e("2", adate);
				// Log.e("3", classid);
				// url.openStream();
				// URL url = new URL(addr);

				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();

				InputStream is = null;
				ByteArrayOutputStream baos = null;
				Log.e("login 응답 : ", "접속0");
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					Log.e("login 응답 : ", "접속1");
					is = conn.getInputStream();
					Log.e("login 응답 : ", "접속2");
					baos = new ByteArrayOutputStream();
					Log.e("login 응답 : ", "접속3");
					byte[] byteBuffer = new byte[1024];
					byte[] byteData = null;
					int nLength = 0;
					Log.e("login 응답 : ", "접속4");
					while ((nLength = is.read(byteBuffer)) > 0) {
						baos.write(byteBuffer, 0, nLength);
					}
					Log.e("login 응답 : ", "접속5");
					byteData = baos.toByteArray();
					Log.e("login 응답 : ", "접속6");
					String response = new String(byteData, "euc-kr");
					// String response = new String(byteData);
					Log.e("login 응답 : ", "접속7");
					Log.e("login 응답 : ", response);
					if (response == null || response.equals("")) {
						throw new Exception("Response is NULL!");
					}
					
					tempquestion=response;

				} else {
					Log.e("login 응답 : ", "접속 안됨");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}
	
	
	void noticeActivity(){
		Intent intent = new Intent(this, noticeActivity1.class);
		startActivity(intent);
	}
}

class QuestionData {
	String qNumber;
	String qTitle;
	String qTeacher;
	String qAnswer;

	public QuestionData(String num, String title, String teacher, String yn) {
		// TODO Auto-generated constructor stub
		qNumber = num;
		qTitle = title;
		qTeacher = teacher;
		qAnswer = yn;
	}

}

class QuestionAdapter extends BaseAdapter {
	Context maincon;
	LayoutInflater Inflater;
	ArrayList<QuestionData> arSrc;
	int layout;

	public QuestionAdapter(Context context, int alayout, ArrayList<QuestionData> aarSrc) {
		maincon = context;
		Inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		arSrc = aarSrc;
		layout = alayout;
	}

	public int getCount() {
		return arSrc.size();
	}

	public String getItem(int position) {
		return arSrc.get(position).qTitle;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		if (convertView == null) {
			convertView = Inflater.inflate(layout, parent, false);
		}

		TextView qNumberTxt = (TextView) convertView.findViewById(R.id.qNumber);
		qNumberTxt.setText(arSrc.get(position).qNumber);

		TextView qTitleTxt = (TextView) convertView.findViewById(R.id.qTitle);
		qTitleTxt.setText(arSrc.get(position).qTitle);

		TextView qTeacherTxt = (TextView) convertView
				.findViewById(R.id.qTeacher);
		qTeacherTxt.setText(arSrc.get(position).qTeacher);

		TextView qAnswerTxt = (TextView) convertView.findViewById(R.id.qAnswer);
		qAnswerTxt.setText(arSrc.get(position).qAnswer);

		return convertView;
	}

}
